﻿#set($PKColumnName="")
#foreach($u in $TableInfos)
#beforeall
#each
#foreach($c in $u.ColumnInfos)  
#beforeall
#each 
#if($c.IsPK=="True")
#set($PKColumnName=  $c.ColumnName)
#end
#end
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BrainSoft.Common;
using BrainSoft.Entity;
using BrainSoft.Models;
using BrainSoft.Models.Interfaces;
namespace BrainSoft.Web.Manager.SystemMain.${u.TabName}
{
    public partial class Manager : System.Web.UI.Page
    {
        /// <summary>
        /// 获取值
        /// </summary>
        string CMD = (string)Utils.GetQueryAndFormString("CMD",MethodType.Get, 10, 1, DataType.CharAndNum);
        int eId = (int)Utils.GetQueryAndFormString("EId", MethodType.Get, 255, 0, DataType.Int);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OnStart();
            }
        }

        private void OnStart()
        {
            
            switch (CMD)
            {
                case "New":
                    break;
                case "Edit":
                    InputData();
                    break;
            }
        }
      

        /// <summary>
        /// 绑定起始数据
        /// </summary>
        private void InputData()
        {
            if (eId == 0)
                return;
            IDataService<${u.TabName}Entity> dataService = ServiceBuilder.Build${u.TabName}Service();
            ${u.TabName}Entity ${u.TabName.ToLower()}Entity = dataService.Look(eId);
#foreach($c in $u.ColumnInfos)  
#each 
#if($c.IsPK!="True")
#if($c.TypeName=="bit"  || $c.TypeName=="tinyint")
            rbList${c.ColumnName}.SelectedValue = ${u.TabName.ToLower()}Entity.${c.ColumnName}.ToString();
#elseif($c.TypeName=="datetime")
            tb${c.ColumnName}.Text =(${u.TabName.ToLower()}Entity.${c.ColumnName} != null ? ((DateTime)${u.TabName.ToLower()}Entity.${c.ColumnName}).ToString("yyyy-MM-dd") : ""); 
#elseif($c.TypeName=="int")
            tb${c.ColumnName}.Text = ${u.TabName.ToLower()}Entity.${c.ColumnName}.ToString();
#else
            tb${c.ColumnName}.Text = ${u.TabName.ToLower()}Entity.${c.ColumnName};
#end
#end
#end 
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ${u.TabName}Entity ${u.TabName.ToLower()}Entity = new ${u.TabName}Entity();
            IDataService<${u.TabName}Entity> dataService = ServiceBuilder.Build${u.TabName}Service();
#foreach($c in $u.ColumnInfos)  
#beforeall
#each 
#if($c.IsPK!="True")
#if($c.TypeName=="bit"  || $c.TypeName=="tinyint")
            ${u.TabName.ToLower()}Entity.${c.ColumnName} = (bool)Utils.GetQueryAndFormString(rbtList${c.ColumnName}.ClientID, MethodType.Post, ${c.Length}, #if(${c.cisNull}!="True") 1 #else 0#end, DataType.Byte);
#else
            ${u.TabName.ToLower()}Entity.${c.ColumnName} = (${c.TypeCSharpName})Utils.GetQueryAndFormString(tb${c.ColumnName}.ClientID,MethodType.Post, ${c.Length}, #if(${c.cisNull}!="True") 1 #else 0#end, DataType.${c.TypePostName});
#end
#end
#end
            switch (CMD)
            {
                case "New":
                    if(dataService.Insert(${u.TabName.ToLower()}Entity))
                    {
                        MessageBox mbox=new MessageBox();
                        mbox.MTitle="添加成功";
                        mbox.DialogName = "add";
                        mbox.MType = MessageType.Colse;
                        mbox.MUrl = "/Manager/SystemMain/${u.TabName}/default.aspx?CMD=Look";
                        EventMessage.ShowMessageBox(mbox);
                        
                    }
                    else 
                    {
                        MessageBox mbox = new MessageBox();
                        mbox.MTitle = "添加失败";
                        mbox.MType = MessageType.GoBack;
                        EventMessage.ShowMessageBox(mbox);
                    }
                    break;
                case "Edit":
                    ${u.TabName.ToLower()}Entity.${PKColumnName} = eId;
                    if (dataService.Update(${u.TabName.ToLower()}Entity))
                    {
                        MessageBox mbox = new MessageBox();
                        mbox.MTitle = "编辑成功";
                        mbox.DialogName = "edit";
                        mbox.MType = MessageType.Colse;
                        mbox.MUrl = "/Manager/SystemMain/${u.TabName}/default.aspx?CMD=Look";
                        EventMessage.ShowMessageBox(mbox);
                    }
                    else
                    {
                        MessageBox mbox = new MessageBox();
                        mbox.MTitle = "编辑失败";
                        mbox.MType = MessageType.GoBack;
                        EventMessage.ShowMessageBox(mbox);
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
#end